// pages/admin/map.js
const {
  getAdminRemoteRecord,
} = require("../../utils/custom_loading")

const {
  getCheckpointList,
  saveCheckpointList,
  getCheckpointByID
} = require("../../utils/initdata")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    gps: ["30.639883,104.324013", "30.638738,104.323371", "30.637332,104.325242", "30.633947,104.330259", "30.637206,104.332705", "30.637865,104.329098"],
    markers: [],
    polygon: [],
    lineData: [],
    polyline: [],
    points: [],
    distanceArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // wx.setStorageSync('admin', true)
    // return this.getUserList();
    this.showCheckpointGPS()
  },

  showCheckpointGPS() {
    const checkpoint_list = getCheckpointList()
    const checkpoint_marker_list = []
    checkpoint_list.forEach(function (checkpoint, index) {
      if (checkpoint.gps === '') return
      console.log(checkpoint)
      const gps = checkpoint.gps.split(',')
      checkpoint_marker_list.push({
        id: index,
        latitude: gps[0],
        longitude: gps[1],
        width: '25px',
        height: '35px',
        callout: {
          content: checkpoint.code+'-'+checkpoint.name,
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      })
    })

    console.log(checkpoint_marker_list)
    // 显示位置
    this.setData({
      markers: checkpoint_marker_list,
    })
  },

  async getUserList() {
    wx.showLoading({
      title: '数据载入中...',
    });

    // 获取列表
    const res = await getAdminRemoteRecord()

    // 关闭等待消息
    wx.hideLoading();

    const userList = res.result.data

    // 将用户信息关联到marker
    let userMarker = []
    // console.log('user list', userList)
    userList.forEach(function (userinfo, index) {
      // 跳过没有GPS信息的用户
      if (userinfo.gps.length == 0) return

      // 将用户GPS数据添加到Marker
      // console.log('userinfo', userinfo)
      let gps = userinfo.gps.split(",")
      userMarker.push({
        id: index,
        latitude: gps[0],
        longitude: gps[1],
        width: '25px',
        height: '35px',
        callout: {
          content: userinfo.nickname,
          padding: 5,
          fontSize: 12,
          textAlign: 'center',
          display: 'ALWAYS',
          borderRadius: 5,
          borderWidth: 1,
          bgColor: '#ffffff',
          borderColor: "#c3c3c3"
        },
      })
    })

    // 电子围栏
    let polyline = []
    this.data.gps.forEach(function (item) {
      let gps = item.split(',')
      polyline.push({
        latitude: gps[0],
        longitude: gps[1]
      })
    })
    // console.log(polyline)

    // 显示用户位置
    this.setData({
      markers: userMarker,
      polygon: [{
        points: polyline,
        fillColor: "#4F94CD33",
        strokeColor: "#fa0000",
        strokeWidth: 2,
        zIndex: 5,
      }]
    })

    // console.log(this.data.polygon)
  },

  resetGame() {
    console.log('reset')
    wx.clearStorage()
    wx.redirectTo({
      url: '/pages/main/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})